<?php
namespace Home\Controller;
use Think\Controller;

class CodeController extends Controller {
	private $jiqiguanli = 'jiqiguanli';
	function _initialize() {
		$id = I('get.id');
		if (!$id) {
			//非法操作
			exit("{0}");
		}
		$map['jiqima'] = $id;
		$t = M($this->jiqiguanli)->where($map)->getField('jiqima');
		if (!$t) {
			//机器码不存在
			exit("{0}");
		}
	}
	//选择支付方式
	public function index() {
		if (time() < $this->getStatic()) {
			$this->assign('static', '机器正在使用中，暂停支付~');
		}
		$this->display();
	}
	//获取机器使用时间状态
	private function getStatic() {
		$map['code'] = I('get.id');
		$map['start'] = 0;
		$end_time = M('pay')->where($map)->order('id desc')->getField('end_time');
		if ($end_time) {
			return $end_time;
		}
	}
	public function setmoney() {
		$money = I('post.money');
		$_SESSION['weixinpayMoney'] = $money;
		//session(array('weixinpayMoney' => $money, 'expire' => 60 * $money));
		echo $_SESSION['weixinpayMoney'];
	}
	public function pay() {
		exit;
		if (IS_POST) {
			// if (!M('pay')->autoCheckToken($_POST)) {
			// 	//表单令牌错误~
			// 	$this->error('表单令牌错误~', U('Code/index', array('id' => I('get.id'))));
			// }
			$getid = I('get.id');

			if (time() < $this->getStatic()) {
				$this->error('机器正在使用中，暂停支付~', U('Code/index', array('id' => $getid)));
			}

			$money = I('post.money', '', 'intval');
			$time = time();
			$id = I('get.id');
			$end = $time + (60 * $money);
			// echo date('Y-m-d H:i:s', $time);
			// echo date('Y-m-d H:i:s', $end);
			$data['code'] = $id;
			$data['money'] = $money;
			$data['pay_type'] = I('post.payType');
			$data['open_time'] = $time; //开始时间
			$data['end_time'] = $end; //结束时间
			$type = M('pay')->add($data);
			if ($type) {
				$url = U('Code/msgInfo', array('id' => $id, 'type' => $type));
				exit(json_encode(array('stat' => 1, 'url' => $url, 'msg' => '支付成功')));
				//$this->redirect(, 2, '支付成功，正在跳转...');
			} else {
				$url = U('Code/index', array('id' => $id));
				exit(json_encode(array('stat' => 0, 'url' => $url, 'msg' => '支付失败')));
				//$this->error('支付失败', U('Code/index', array('id' => $id)));
			}
		}

	}
	//提示信息
	public function msgInfo() {
		$this->display();
	}

	// /成功返回 {2}    其它返回{0}  超过使用使用时间，即设置失效
	//http://www.hbtxgk.cn/Code/getTypeInfo/id/99999999.html
	public function getTypeInfo() {
		$map['code'] = I('get.id');
		$map['start'] = 0;
		//获取状态时，时间超过使用结束时间，就失效
		$end_time = M('pay')->where($map)->order('id desc')->getField('end_time');
		if (time() > $end_time) {
			//超过有效使用时间
			$st = M('pay')->where($map)->save(array('start' => 1));
		}
		//再次重新获取最新的
		$money = M('pay')->where($map)->order('id desc')->getField('money');
		if ($money) {
			exit("{{$money}}");
		} else {
			exit("{0}");
			//echo json_encode(array('type' => '0', 'msg' => '非法操作'));
		}
	}
	//测试微信支付
	public function weixinpay() {
		if (time() < $this->getStatic()) {
			$this->error('机器正在使用中，暂停支付~', U('Code/index', array('id' => $getid)));
		}
		$money = $_SESSION['weixinpayMoney'];
		$id = I('get.id');
		if (!$money) {
			$this->error('非法操作~');
		}
		Vendor('Wxpay.WxPay#Api');
		Vendor('Wxpay.WxPay#JsApiPay');
		$moeny = $money;
		//最低一元起
		$tal_fee = $moeny * 100;
		//①、获取用户openid
		$tools = new \JsApiPay();
		$openId = $tools->GetOpenid();
		//②、统一下单
		$url = root() . U('Home/index/notify', array('id' => $id, 'money' => $money));
		$input = new \WxPayUnifiedOrder();
		$input->SetBody("机器号:{$id}");
		$input->SetAttach("{$id}");
		$input->SetOut_trade_no(\WxPayConfig::MCHID . date("YmdHis"));
		$input->SetTotal_fee($tal_fee);
		$input->SetTime_start(date("YmdHis"));
		$input->SetTime_expire(date("YmdHis", time() + 600));
		$input->SetGoods_tag("SetGoods_tag:{$id}");
		$input->SetNotify_url($url);
		$input->SetTrade_type("JSAPI");
		$input->SetOpenid($openId);
		$order = new \WxPayApi();
		$infos = $order->unifiedOrder($input);
		if (count($infos) < 5) {
			exit(json_encode(array('msg' => '禁止刷新')));
		}
		$jsApiParameters = $tools->GetJsApiParameters($infos);
		$this->assign('jsApiParameters', $jsApiParameters);
		$this->assign('moeny', $moeny);
		$this->display();
	}

	/* 支付回调地址 只要有数据回来  那说明   肯定是支付成功的   不支付成功不会回调*/
	// public function notify() {
	// 	$xml = $GLOBALS["HTTP_RAW_POST_DATA"];
	// 	/* 如果不回复这个xml  微信会给我们发送三次xml */
	// 	$su = '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
	// 	echo $su;
	// }
}